package banner

import (
	"context"
	"group-buy/group_buy/internal/models"
	"group-buy/group_buy/internal/svc"
	"group-buy/group_buy/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type SaveBannerLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewSaveBannerLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SaveBannerLogic {
	return &SaveBannerLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *SaveBannerLogic) SaveBanner(req *types.Banner) (resp *types.Banner, err error) {
	m := &models.Banner{
		Id:    req.Id,
		Title: req.Title,
		Path:  req.Path,
		Url:   req.Url,
		Sort:  req.Sort,
	}
	if m.Id > 0 {
		_, err = l.svcCtx.Engine.ID(m.Id).Update(m)
		if err != nil {
			return nil, err
		}
	} else {
		_, err = l.svcCtx.Engine.Insert(m)
		if err != nil {
			return nil, err
		}
	}
	req.Id = m.Id

	return req, nil
}
